<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Youzi
 * Date: 12-11-26
 * Time: 下午9:33
 * To change this template use File | Settings | File Templates.
 */
class AttendanceModel extends Model
{
    static  $_instance   = null;
    private $_table     = 'crm_attendance';

    static function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function getAll()
    {
        return $this->sql("select * from ".$this->_table." order by id desc ")->s();
    }
    /**
     * 会议记录列表
     *
     * @param  string $sorts
     * @param  int  $page
     * @param  int  $pagesize
     * @return array
     */
    public function getList($sorts = 'id', $page = 1, $pagesize = 10)
    {
        $sql = 'select * from '.$this->_table.' where stop=0 order by '.$sorts.' asc ';
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;
        $list = $this->sql($sql.$limit)->s();
        return $list;
    }

    public function getCount()
    {
        $sql = "";
        $list = $this->sql('select count(*) as c from '.$this->_table.' where stop=0 ')->s(1);
        return $list['c'];
    }

    public function getUserList($page=1, $pageSize=10)
    {
        $sql  = "select su.*,ca.id as agency_id,ca.name as agency,cd.id as department_id,cd.name as department ";
        $sql .= "from sys_user as su left join crm_agency as ca on ca.stop=0 and su.agency_id=ca.id ";
        $sql .= "                    left join crm_department as cd on cd.stop=0 and su.department_id=cd.id ";
        $sql .= "where su.ugroup=2";
        $sql .= " limit ".($page-1)*$pageSize.",".$pageSize;
        return $this->sql($sql)->s();
    }

    public function getUserCount()
    {
        $sql  = "select count(*) as n ";
        $sql .= "from sys_user as su left join crm_agency as ca on ca.stop=0 and su.agency_id=ca.id ";
        $sql .= "left join crm_department as cd on cd.stop=0 and su.department_id=cd.id ";
        $sql .= "where su.ugroup=2";
        return $this->sql($sql)->s(1);
    }

    public function getUserRow($condition, $field='*')
    {
        $sql = "select $field from sys_user where $condition limit 1";
        return $this->sql($sql)->s(1);
    }

    /**
     * 添加会议记录
     *
     * @param  array    $data
     * @return bool
     */
    public function add($data)
    {
        $sql = insertSql($this->_table, $data);
        return $this->sql($sql)->i();
    }

    /**
     * 修改会议记录
     *
     * @param  array    $data
     * @param  int      $id
     * @return bool
     */
    public function edit($data, $id)
    {
        $sql = updateSql($this->_table, $data, 'id='.$id);
        return $this->sql($sql)->u();
    }

    /**
     * 删除会议记录
     *
     * @param  int  $id
     * @return bool
     */
    public function del($id, $in=true)
    {
        if ($in) {
            $sql = "delete from ".$this->_table." where id=".$id;
        } else {
            $sql = "delete from ".$this->_table." where id in (".$id.")";
        }

        return $this->sql($sql)->d();
    }

    /**
     * 获取会议记录详情
     *
     * @param  int  $id
     * @return array
     */
    public function getInfo($id, $in=true)
    {
        if ($in) {
            $row = $this->sql('select * from '.$this->_table.' where id= '.$id)->s(1);
        } else {
            $row = $this->sql('select * from '.$this->_table.' where id in ('.$id.')')->s();

        }
        return $row;
    }

    public function getByRow($condition, $field='*')
    {
        $sql = "select $field from ".$this->_table." where $condition limit 1";
        return $this->sql($sql)->s(1);
    }

    public function getRows($condition = '', $page=1, $pageSize=10)
    {
        $val = array();
        $sql = "select count(*) as total
                from crm_attendance as a
                left join crm_staff as b on a.staff_id=b.id
                left join crm_agency as c on b.agency_id=c.id
                left join crm_department as d on b.department_id=d.id
                where a.stop=0 $condition";
        $row = $this->sql($sql)->s(1);
        $val['total'] = $row['total'];

        $sql = "select a.*,b.name,c.name as agency_name,d.name as department_name
                from crm_attendance as a
                left join crm_staff as b on a.staff_id=b.id
                left join crm_agency as c on b.agency_id=c.id
                left join crm_department as d on b.department_id=d.id
                where a.stop=0 $condition";
        $sql .= " order by a.id desc limit ".($page-1)*$pageSize.",".$pageSize;
        $val['list'] = $this->sql($sql)->s();
        return $val;
    }
}